package JavaDay6;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class DouDiZhuPokerSort {
	public static void main(String[] args) {
		ArrayList<String> poker=new ArrayList<>();
		String[] colors={"♥","♦","♠","♣"};
		String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
		poker.add("大王");
		poker.add("小王");
		for(String number:numbers){
			for(String color:colors){
				poker.add(color+number);
			}
		}
		System.out.println(poker);
		//新牌，进行map排序
		ArrayList<String> newPoker = new ArrayList<>(poker);
		//洗牌
		Collections.shuffle(poker);
		System.out.println(poker);
		Player p1=new Player();
		p1.setName("老王");
		Player p2=new Player();
		p2.setName("老李");
		Player p3=new Player();
		p3.setName("老六");
		ArrayList<String> diPai=new ArrayList<>();
		for(int i=0;i<poker.size();i++){
			String p = poker.get(i);
			if(i>=51){
				diPai.add(p);
			}else if(i%3==0){
				p1.getPoker().add(p);
			}else if(i%3==1){
				p2.getPoker().add(p);
			}
			else if(i%3==2){
				p3.getPoker().add(p);
			}
		}
		
		
		Map<Integer, String> pokerMap = new HashMap<Integer, String>();
	
		//给新牌进行排序
		for (int i = 0; i < poker.size(); i++) {
			pokerMap.put(i, newPoker.get(i));
		}
		//对每个玩家手中的牌进行排序
		pokerSort(p1,pokerMap);
		pokerSort(p2,pokerMap);
		pokerSort(p3,pokerMap);
	
		
		System.out.println(p1.getName()+":"+p1.getPoker());
		System.out.println(p2.getName()+":"+p2.getPoker());
		System.out.println(p3.getName()+":"+p3.getPoker());
	}

	
	private static void pokerSort(Player p1, Map<Integer, String> pokerMap) {
		//通过值找到对应的键，集合
		List<Integer> list = new ArrayList<>();
		list = getKey(p1,list,pokerMap);
		//循环修改每个人手中的牌，进行排序，通过键，得到键值，
		for (int k = 0; k < p1.getPoker().size(); k++) {
			p1.getPoker().set(k, pokerMap.get(list.get(k)));
		}

	}



	//得到对应键的集合
	private static List<Integer> getKey(Player p1, List<Integer> list, Map<Integer, String> pokerMap) {
		for (int i = 0; i < p1.getPoker().size(); i++) {
			String value = p1.getPoker().get(i);
			for (Entry<Integer, String> entry : pokerMap.entrySet()){
				if (value.equals(entry.getValue())){
					list.add(entry.getKey());
					break;
				}
			}
			
		}
		
		Collections.sort(list);
		
		return list;
	}

}
